<?php
class BetTotal_6h
{

    public $disk = "a";
    public $lotto_id = 0;
    public $table_name = "bettotal_6h_0";
    public $db = NULL;
    public $tbl = "6h";
    public $disk_code = array
    (
        0 => "",
        1 => "a",
        2 => "b",
        3 => "c",
        4 => "d"
    );
    public $colors = array
    (
        0 => "",
        1 => "red",
        2 => "blue",
        3 => "green"
    );
    public $cid = -1;

    public function BetTotal_6h( $lotto_id = "", $cid = -1 )
    {
        if ( $cid < 0 )
        {
            $this->db = new DB();
        }
        else
        {
            $this->db = new DB_CEN( $cid );
        }
        if ( $lotto_id != "" )
        {
            $tempTblName = "bettotal_{$this->tbl}_{$lotto_id}";
        }
        if ( $this->chkTbl( $tempTblName ) )
        {
            $this->table_name = $tempTblName;
        }
        else
        {
            exit( "error bettotal 0" );
            $this->table_name = "bettotal_{$this->tbl}_0";
        }
    }

    public function getDataListTotal( $lotto_id, $bettypeStr, $showid, $split = 0, $diskStr = "" )
    {
        $rs = new RecordSet();
        $reArr = array();
        if ( $split == 0 )
        {
            $amountstr = "amount1{$diskStr}";
            $amountstr2 = "amount3{$diskStr}";
            $back1str = "back1{$diskStr}";
        }
        else
        {
            $amountstr = "amount2{$diskStr}";
            $amountstr2 = "amount4{$diskStr}";
            $back1str = "back3{$diskStr}";
        }
		
        $back2str = "sum(back2{$diskStr})";
        $sql = "SELECT bettype, sum(totalnum{$diskStr}) as totalnum , sum({$amountstr}) as amount, sum({$amountstr2}) as amount_rate, sum({$back1str}) as back1, {$back2str} as back2 FROM {$this->table_name}";
        $sql .= " WHERE user_id='{$showid}' AND bettype IN ({$bettypeStr}) AND lotto_id='{$lotto_id}'";
        $sql .= " GROUP BY lotto_id, bettype";
        $rs->open( $sql, 0, 20 );
        return $rs;
    }

    public function getDataListTotal2( $lotto_id, $bettypeStr, $showid, $split = 0, $diskStr = "" )
    {
        $reArr = array();
        $bettype = $bettypeStr;
        if ( $split == 0 )
        {
            $amountstr = "amount1".$diskStr;
            $amountstr2 = "amount3".$diskStr;
            $back1str = "back1".$diskStr;
        }
        else
        {
            $amountstr = "amount2".$diskStr;
            $amountstr2 = "amount4".$diskStr;
            $back1str = "back3".$diskStr;
        }
        $sql = "SELECT sum(totalnum{$diskStr}) as totalnum , sum({$amountstr}) as amount, sum({$amountstr2}) as amount_rate, sum({$back1str}) as back1, sum(back2{$diskStr}) as back2, MAX(updatetime) as updatetime FROM {$this->table_name}";
        $sql .= " WHERE user_id='{$showid}' AND bettype='{$bettype}' AND lotto_id='{$lotto_id}'";
        $result = $this->db->query( $sql );
        if ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
        {
            return $row;
        }
        return false;
    }

    public function getDataListTotalSQL( $lotto_id, $bettypeStr, $showid, $split = 0, $diskStr = "", $whereStr = "" )
    {
        $rs = new RecordSet();
        $reArr = array();
        if ( $split == 0 )
        {
            $amountstr = "amount1{$diskStr}";
            $amountstr2 = "amount3{$diskStr}";
            $back1str = "back1{$diskStr}";
        }
        else
        {
            $amountstr = "amount2{$diskStr}";
            $amountstr2 = "amount4{$diskStr}";
            $back1str = "back3{$diskStr}";
        }
        $back2str = "sum(back2{$diskStr})";
        $sql = "SELECT bettype, sum(totalnum{$diskStr}) as totalnum , sum({$amountstr}) as amount, sum({$amountstr2}) as amount_rate, sum({$back1str}) as back1, {$back2str} as back2 FROM {$this->table_name}";
        $sql .= " WHERE user_id='{$showid}' AND bettype IN ({$bettypeStr}) AND lotto_id='{$lotto_id}' {$whereStr}";
        $sql .= " GROUP BY lotto_id, bettype";
        return $sql;
    }

    public function getDataListByNumSsSQL( $lotto_id, $bettype, $showid, $disk = "", $amount = "0", $ratemod = 2 )
    {
        if ( $amount == "0" )
        {
            $amountstr = "amount1{$disk}";
            $amountstr2 = "amount3{$disk}";
            $back1str = "back1{$disk}";
        }
        else
        {
            $amountstr = "amount2{$disk}";
            $amountstr2 = "amount4{$disk}";
            $back1str = "back3{$disk}";
        }
        $back2Str = "back2{$disk}";
        $ratemodstr = $ratemod == 1 ? "rate1{$disk}" : "rate2{$disk}";
        $sql = "SELECT numbers, bettype ";
        $sql .= " , totalnum{$disk} as totalnum ";
        $sql .= " , {$amountstr} as amount ";
        $sql .= " , {$amountstr2} as amount_rate ";
        $sql .= " , {$ratemodstr} as rate ";
        $sql .= " ,{$back1str} as back1 ";
        $sql .= " ,{$back2Str} as back2 ";
        $sql .= " ,updatetime ";
        $sql .= " FROM {$this->table_name} ";
        $sql .= " WHERE lotto_id='{$lotto_id}' AND user_id='{$showid}' AND bettype IN ({$bettype})";
		
        return $sql;
    }

    public function getDataListByNumSs( $lotto_id, $bettype, $numberstr, $showid, $disk = "", $amount = "0", $ratemod = 2 )
    {
        if ( 0 - 1 < $this->cid )
        {
            $rs = new RecordSet_CEN( $this->cid );
        }
        else
        {
            $rs = new RecordSet();
        }
        if ( $amount == "0" )
        {
            $amountstr = "amount1".$disk;
            $amountstr2 = "amount3".$disk;
        }
        else
        {
            $amountstr = "amount2".$disk;
            $amountstr2 = "amount4".$disk;
        }
        $ratemodstr = $ratemod == 1 ? "rate1".$disk : "rate2".$disk;
        $sql = "SELECT numbers, totalnum{$disk} as totalnum, {$amountstr} as amount, {$amountstr2} as amount_rate, {$ratemodstr} as rate, updatetime FROM {$this->table_name}";
        $sql .= " WHERE lotto_id={$lotto_id} AND user_id={$showid} AND bettype={$bettype}";
        if ( $numberstr != "" )
        {
            $sql .= " AND numbers IN ({$numberstr})";
        }
        $rs->open( $sql );
        return $rs;
    }

    public function chkTbl( $tblName )
    {
        $sql = "SHOW TABLES LIKE '{$tblName}'";
        $result = $this->db->query( $sql );
        if ( 0 < mysql_num_rows( $result ) )
        {
            return true;
        }
        return false;
    }

}

?>